<?php
namespace app\index\controller;

class Products extends Common
{
	//产品介绍
    public function index()
    {
    	 //logo
        $logo=db('logo')->where('caro_states',1)->column('logo_img');
        $this->assign('logo',$logo[0]);
        //轮播图
        $imgdata=db('carousel')->where('caro_states',1)->field('caro_img')->select();
        //halt($imgdata);
        $this->assign('imgdata',$imgdata);
        //分类导航
        $typedata=db('types')->where('type_pid',1)->select();
        foreach($typedata as $k=>$v)
        {
            $v['typeson']=db('types')->where('type_pid',$v['type_id'])->select();
            $typedata[$k]=$v;
        }
        //halt($typedata);
        $this->assign('typedata',$typedata);
        //产品展示
        
         $protype=db('protypes')->where('protype_states',1)->where('protype_pid',1)->select();
         foreach($protype as $k=>$v)
        {
            $v['proson']=db('protypes')->where('protype_pid',$v['protype_id'])->select();
            $protype[$k]=$v;
        }
        //halt($protype);
        //halt($protype);
    	//判断
    	//type_pid
    	$type_pid=input('param.types_pid');
        $wenid=input('param.wen_id');
        if($type_pid=="")
        {
             $proson=db('products')->where('prod_states',1)->select();
        }else
        {
        	//根据判断获得自己和自己的子集
        	if($wenid=="")
        	{
        		$res=$this->art_son($type_pid);
        		//halt($res);
        		$proson=db('products')->where('prod_states',1)->whereIn('protype_pid',$res)->select();
        	}else
        	{
        		$proson=db('products')->where('prod_states',1)->where('protype_pid',$wenid)->select();
        	}
        }


        //halt($protype);
        $this->assign('protype',$protype);
        $this->assign('proson',$proson);

        return $this->fetch();
    }
     /**
    * 获得自己和自己的子集
    */
   public function art_son($type_id)
   {
   		$allData=db('protypes')->select();
   	   //获得自己和自己子集的cate_id
   	   $sonDate=$this->getSonId($allData,$type_id);
   	   //把自己id加进去
   	   $sonDate[]=$type_id;
   	   return $sonDate;
   }
	
   /**
    * 获得子集type_id
    */
   public function getSonId($allData,$type_id)
   {
   		static $tmp=array();
   		foreach($allData as $k=>$v)
   		{
   		   if($type_id==$v['protype_pid']){
   		   	 $tmp[]=$v['protype_id'];
   		   	 $this->getSonId($allData,$v['protype_id']);
   		   }
   		  
   		}
   		return $tmp;
   	}
	
}
